package com.huixh.train.common.context;

import com.huixh.train.common.resp.MemberLoginResp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author huixh
 */
public class LoginMemberContext {
    private final static Logger logger = LoggerFactory.getLogger(LoginMemberContext.class);

    private static final ThreadLocal<MemberLoginResp> MEMBER_LOGIN_RESP_THREAD_LOCAL = new ThreadLocal<>();

    public static void setMemberLoginResp(MemberLoginResp memberLoginResp) {
        MEMBER_LOGIN_RESP_THREAD_LOCAL.set(memberLoginResp);
    }

    public static MemberLoginResp getMemberLoginResp() {
        return MEMBER_LOGIN_RESP_THREAD_LOCAL.get();
    }

    public static void clear() {
        MEMBER_LOGIN_RESP_THREAD_LOCAL.remove();
    }

    public static Long getMemberId() {
        try {
            return MEMBER_LOGIN_RESP_THREAD_LOCAL.get().getId();
        } catch (Exception e) {
            logger.error("获取会员ID失败", e);
            throw e;
        }
    }
}
